package com.qtummatrix.service.impl;

import com.qtummatrix.dao.SaveMoudleDao;
import com.qtummatrix.entity.ModelEntity;
import com.qtummatrix.entity.ModelMoudle;
import com.qtummatrix.entity.MoudleEntity;
import com.qtummatrix.entity.QuestionsEntity;
import com.qtummatrix.service.QuestionsMoudleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by Gongshiyong  2019-12-07 11:31
 */
@Service("questionsMoudleService")
public class QuestionsMoudleServiceImpl implements QuestionsMoudleService {

    @Autowired
    @Qualifier("saveMoudleDao")
    private SaveMoudleDao saveMoudleDao;
    @Override
    public List<QuestionsEntity> get_QuestionsByScope(int scopeid) {
        List<QuestionsEntity> list=saveMoudleDao.get_questionsByScope(scopeid);

        return list;
    }

    @Override
    public String add_QuestionsMoudle(MoudleEntity moudleEntity, int[] questionsid) {
        boolean b=saveMoudleDao.add_Moudle(moudleEntity);
        if (!b){
            return "名称重复，添加失败！";
        }
        int moudleid=0;
        if (moudleEntity.getId()==0){
             moudleid=saveMoudleDao.get_moudleid(moudleEntity.getMoudlename());
        }
       else {
             moudleid=moudleEntity.getId();
        }
        saveMoudleDao.add_questionsMoudle(moudleid,questionsid);

        return "保存成功";
    }

    @Override
    public List<MoudleEntity> get_Moudles(int scopeid, String name, int n) {

        return saveMoudleDao.get_Moudle(scopeid,name,n);
    }

    @Override
    public int get_MoudleSumpage(int scopeid,String name) {
        double pages=saveMoudleDao.get_MoudleSumpage(scopeid,name);
        int sumpage=(int) Math.ceil(pages/11);
        return sumpage;
    }

    @Override
    public List<QuestionsEntity> get_QuestionsInfoByMoudleID(int moudleid) {

        List<Integer> idlist=saveMoudleDao.get_QuestionIDByMoudleID(moudleid);
        List<QuestionsEntity> list=saveMoudleDao.get_QuestionByID(idlist);
        return list;
    }

    @Override
    public void delmoudle(int moudleid) {
        saveMoudleDao.del_moudle(moudleid);
    }

    @Override
    public Map<Integer,Object> getGoodsMoudleInfo() {
        List<ModelEntity> modelentities=saveMoudleDao.get_goodsMoudleInfo();
        List<ModelMoudle> modelMoudles=saveMoudleDao.getModelMoudle();
        List<Integer> idlist=new ArrayList<>();
        for (int i=0;i<modelMoudles.size();i++){
            idlist.add(modelMoudles.get(i).getMoudleid());
        }
        List<String> stringList=saveMoudleDao.getQuestionMoudleByID(idlist);
        Map<Integer, Object> map=new HashMap<>();
        map.put(0,modelentities);
        map.put(1,stringList);
        return map;
    }

    @Override
    public List<MoudleEntity> get_AllMoudle() {
        List<MoudleEntity> list=saveMoudleDao.getAllmoudleEntity();

        return list;
    }

    @Override
    public void updateGoodsMoudle(int id, int moudleid) {
        saveMoudleDao.updateGoodsMoudle(id,moudleid);
    }

}
